#include "starfield.h"
#include "starfieldview.h"

StarfieldView::StarfieldView(QWidget *parent)
:QWidget(parent), renderer(0), sf(0)
{
    // Le bon vieux mode 13h (simul�, c'est s�r :p )
    setMinimumSize(320, 200);
    setMaximumSize(320, 200);
    resize(320, 200);

    renderer = SDLHelpers::createQImageCompliantSurface(SDL_SWSURFACE, 320, 200);
}

void StarfieldView::setStarfield(Starfield *starfield)
{
    if(sf)
    {
        disconnect(sf, SIGNAL(starfieldUpdated()));
    }

    sf = starfield;

    if(sf)
    {
        connect(sf, SIGNAL(starfieldUpdated()), this, SLOT(update()));
    }
}

void StarfieldView::setColor(QRgb color)
{
    col = color;
}

void StarfieldView::paintEvent(QPaintEvent *e)
{
    if(renderer && sf)
    {
        sf->drawStarfield(renderer,
            SDL_MapRGBA(renderer->format,
                qRed(col), qGreen(col), qBlue(col), qAlpha(col)));

        QImage im = SDLHelpers::SDLSurfaceToQImage(renderer);

        QPainter p(this);
        p.drawImage(0, 0, im);
    }
}
